<?php
/*
**天时同城退票及验票通知
**2015-06-30
**by pzp
*/
if (!class_exists("go_sql")){
	include("/var/www/html/new/conf/le.je");
	$le=new go_sql();
	$le->connect();
}
include('/var/www/html/new/d/module/api/function.class.php');

$PFT_infunction = new AllFunction();
	
$txt= json_encode($_REQUEST);
logfile($txt,'Note_data');
// $txt = '{"_pid":"123280","method":"validate","format":"json","my_orders_id":"8029287","another_orders_id":"1543478","supplier_id":"120910","code":"1357318432","amount":"32","_sig":"D3861E1DECD409134AF40DEC65FB7334"}';

$arr = json_decode($txt,true);
if($arr['method'] == 'validate'){
	$another_orders_id = $arr['another_orders_id'];
}elseif($arr['method'] == 'refundResult'){
	$another_orders_id = $arr['orders_id'];
} 
//第三方平台订单ID
// $pft_order = select_order($another_orders_id);

$selorder = "select pftOrder from all_api_order where tempOrder='$pft_order' limit 1";
$GLOBALS['le']->query($selorder);
$GLOBALS['le']->fetch_assoc();
$pftOrder = $GLOBALS['le']->f('pftOrder');

if($pftOrder){
	
	$pft_order_xml = $PFT_infunction->Select_PFTorder($pftOrder);
	$UUsalerid  = (string)$pft_order_xml->Rec->UUsalerid;
	$UUtid  = (int)$pft_order_xml->Rec->UUtid;
	$UUcode  = (string)$pft_order_xml->Rec->UUcode;
	$UUtnum  = (int)$pft_order_xml->Rec->UUtnum;
	$UUtprice  = (float)$pft_order_xml->Rec->UUtprice;
	$UUstatus  = (string)$pft_order_xml->Rec->UUstatus;

	$chkIns = 499;
	if($UUstatus==2) $chkIns = 498;
	
	$sel = "select signkey from uu_qunar_use where DockingMode=5 and tid=$UUtid limit 1";
	$GLOBALS['le']->query($sel);
	$GLOBALS['le']->fetch_assoc();		
	$authcode = $GLOBALS['le']->f('signkey');
		
	$_sig = $arr['_sig'];
	$arr_sign = ksort_sign($arr,$authcode);
	if($_sig == $arr_sign){
		$method = $arr['method'];
		if($method == 'validate'){//验票通知
			$terminal=	$PFT_infunction->Unified_Check_IN($UUsalerid,$UUcode,date('Y-m-d H:i:s'),$chkIns);	
			if($terminal == 100){
				$message = '成功';
				$repMsg = 'true';
			}else{
				$message = $terminal;
				$repMsg = "false";
			}
			logfile($pftOrder.'|'.$terminal,'Note_check');
			echo '{"success":"'.$repMsg.'","message":"'.$message.'"}';
		}	
		elseif($method == 'refundResult'){ //退款通知
			// $type  = $arr['type']; // 3退票成功，4退票不通过
			// $orders_id = $arr['orders_id']; //第三方平台订单ID
			// if($type == 3){
				// $str = "update uu_order_fx_details set memo='tstc_Refund_success' where orderid=".$orders_id." limit 1";
				// $GLOBALS['le']->query($str);
				
			// }else{
				// $str = "update uu_order_fx_details set memo='tstc_Refund_fail' where orderid=".$orders_id." limit 1";
				// $GLOBALS['le']->query($str);
			// }
			$message = '成功';
			$repMsg = 'true';
			echo '{"success":"'.$repMsg.'","message":"'.$message.'"}';
		}
	}
	else{
		echo '{"success":"fail","message":"签名校验不通过"}';
	}	
}
else{
	echo '{"success":"fail","message":"此订单不存在"}';
}


//拼接数据及加密
function ksort_sign($array,$authcode){
	unset($array['_sig']);
	ksort($array);
	$o=""; 
	foreach ($array as $k=>$v){ 
		$o.= "$k=".$v."&"; 
	} 
	$post_data=substr($o,0,-1);	
	$_sig = strtoupper(md5($authcode."&".$post_data."&".$authcode));
	return $_sig;
}

function Err_Word($r){
	$errCode = array(
		'105'=>'找不到订单',
		'121'=>'订单状态出错,订单已验证',
		'119'=>'订单状态出错,已取消或其他状态',
		'521'=>'验证间隔时间未到',
		'133'=>'游玩时间未到',
		'134'=>'游玩时间已过',
		'118'=>'终端服务器无响应',
		'1070'=>'订单未支付',
	);
	$eword=$errCode[$r];
	if (!$eword) $eword='其他错误';
	return $eword;
}

function logfile($word,$path){
	$filepath= '/var/www/html/new/d/logs/api/TianShiTongCheng/'.$path.'.txt';
	$word = date("Y-m-d H:i:s")."|".$word;
	if(file_exists($filepath)){
		$maxsize  = 1024*1024*10;
		$filesize = filesize($filepath);
		$filetype = end(explode(".",$filepath));
		$filename = substr($filepath,0,strpos($filepath,'.'));
		if($filesize>$maxsize)
			rename($filepath, $filename.'_'.date('YmdHis').'.'.$filetype);
	}
	$fp = fopen('/var/www/html/new/d/logs/api/TianShiTongCheng/'.$path.'.txt',"a");
	flock($fp, LOCK_EX);
	fwrite($fp,date('Y-m-d H:i:s')."|".$word."\n");
	flock($fp, LOCK_UN);
	fclose($fp);
}

?>